package stack;

import java.util.Stack;

/**
 * @description:
 * @title: CalculatorDemo
 * @author: zp
 * @date: 2021/11/6 17:36
 * 使用栈 实现计算器字符计算
 */
public class CalculatorDemo {
    public static void main(String[] args) throws Exception {
        String expressions="70-2*6+1";
        System.out.println(expression(expressions));
    }

    public static int expression(String expression) throws Exception {
        if(expression==null || expression.trim().equals("")){
            throw new Exception("输入有误");
        }
        Stack<Integer> numStack=new Stack<>();
        Stack<Character> characterStack=new Stack<>();
        String str="";
        char[] cs=expression.toCharArray();
        int index =0;
        int num1;
        int num2;
        int oper;
        char ch=' ';
        int res;
       while (true){
           ch=cs[index];
           if(isOper(ch)){
               if(characterStack.empty()){
                   characterStack.push(ch);
               }else {
                   if(priority(ch)<=priority(characterStack.peek())){
                        num1=numStack.pop();
                        num2=numStack.pop();
                        oper=characterStack.pop();
                        res=cal(num1,num2,oper);
                        numStack.push(res);
                        characterStack.push(ch);
                   }else {
                       characterStack.push(ch);
                   }
               }

           }else {
               str+=ch;
               if(index==cs.length-1){
                   numStack.push(Integer.valueOf(str));
               }else {
                   if(isOper(cs[index + 1])) {
                       numStack.push(Integer.valueOf(str));
                       str = "";
                   }
               }

           }
           index++;
           if(index>=cs.length){
               break;
           }
       }
       while (true){
           if(characterStack.empty()){
               break;
           }
           num1=numStack.pop();
           num2=numStack.pop();
           oper=characterStack.pop();
           res=cal(num1,num2,oper);
           numStack.push(res);
       }
        return numStack.pop();
    }
    /**
     * @description:
     * @author: zp
     * @date: 2021/11/7 15:26
     * @param: oper
     * @return: int
     * 判断输入符号优先级
     */
    public static int priority(int oper){
        if(oper=='*' || oper == '/'){
            return 1;
        }else if(oper=='+' || oper == '-'){
            return 0;
        }else {
            return -1;
        }
    }
    /**
     * @description:
     * @author: zp
     * @date: 2021/11/7 15:26
     * @param: val
     * @return: boolean
     * 判断是否为符号
     */
    public static boolean isOper(char val){
        return  val=='+' || val=='*' || val=='/' || val=='-';
    }
    /**
     * @description:
     * @author: zp
     * @date: 2021/11/7 15:25
     * @param: num1
     * @param: num2
     * @param: oper
     * @return: int
     * 计算
     */
    public static int cal(int num1 ,int num2,int oper){
        int res=0;
        switch (oper){
            case '+':
                res = num1+num2;
                break;
            case '-':
                res = num2-num1;
                break;
            case '*':
                res = num1*num2;
                break;
            case '/':
                res=num2/num1;
                break;
            default:break;
        }
        return res;

    }
}
